package com.iamteer.day07;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyDefaultHandler2 extends DefaultHandler {
	private List<Contact> list = new ArrayList<Contact>();
	public List<Contact> getList() {
		return list;
	}
	private Contact contact;
	private String currentTag;
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		currentTag = qName;
		if ("contact".equals(qName)) {
			contact = new Contact();
			contact.setId(attributes.getValue("id"));
		} 
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		String content = new String(ch, start, length);
		
		if ("name".equals(currentTag)) {
			contact.setName(content);
		} else if ("age".equals(currentTag)) {
			contact.setAge(content);
		} else if ("phone".equals(currentTag)) {
			contact.setPhone(content);
		} else if ("email".equals(currentTag)) {
			contact.setEmail(content);
		} else if ("qq".equals(currentTag)) {
			contact.setQq(content);
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		currentTag = null;
		if ("contact".equals(qName)) {
			list.add(contact);
		}
	}
	
}
